﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;

namespace dPet.Web.Hospital.Controllers
{
	using dPet.Application.DataObjects;
	using dPet.Application.Services;
	using dPet.Web.Hospital.Fliters;
	using dPet.Web.Hospital.ViewModels;
	using Hangerd.Mvc;

	public class ConfigController : HangerdController
	{
		private readonly ITemplateService _templateService;

		public ConfigController(ITemplateService templateService)
		{
			_templateService = templateService;
		}

		[StaffLoginAuth]
		public ActionResult BillTemplateConfig()
		{
			var template = _templateService.GetBillTemplate(LoginStaffModel.Current.HospitalId) ?? 
				new BillTemplateDto
				{
					ShowAddress = true,
					ShowPhoneNumber = true,
					ShowFaxNumber = true
				};

			return View(template);
		}

		[HttpPost]
		[StaffLoginAuth]
		public ActionResult SaveBillTemplateConfig(BillTemplateDto templateDto)
		{
			var loginStaff = LoginStaffModel.Current;
			var result = _templateService.SaveBillTemplate(loginStaff.HospitalId, templateDto, loginStaff);

			return base.AlertAndRedirect(result.Message, "/Config/BillTemplateConfig");
		}

		[StaffLoginAuth]
		public ActionResult PriviewBillTemplate(BillTemplateDto templateDto)
		{
			var tempSaleOrder = new SaleOrderDto
			{
				TotalAmount = 345.67M,
				CompleteTime = new DateTime(2014, 12, 31),
				OrderLines = new List<SaleOrderLineDto>
				{
					new SaleOrderLineDto{ ProductBarcode = "00001", ProductName = "商品00001", Quantity = 3, UnitName = "件", UnitPrice = 1.2M },
					new SaleOrderLineDto{ ProductBarcode = "00002", ProductName = "商品00002", Quantity = 4, UnitName = "件", UnitPrice = 3.4M },
					new SaleOrderLineDto{ ProductBarcode = "00003", ProductName = "商品00003", Quantity = 5, UnitName = "件", UnitPrice = 5.6M },
					new SaleOrderLineDto{ ProductBarcode = "00004", ProductName = "商品00004", Quantity = 6, UnitName = "件", UnitPrice = 7.8M },
					new SaleOrderLineDto{ ProductBarcode = "00005", ProductName = "商品00005", Quantity = 7, UnitName = "件", UnitPrice = 10M }
				}
			};

			return View("~/Views/SaleOrder/Bill.cshtml", new SaleOrderBillModel
			{
				SaleOrder = tempSaleOrder,
				Template = templateDto
			});
		}
	}
}
